package com.first.teacher.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.first.common.pojo.EduVideo;
import com.first.teacher.mapper.VideoMapper;
import com.first.teacher.service.VideoService;
import com.first.teacher.vo.InsertVideoVo;
import com.first.teacher.vo.UpdataVideoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @program: finrsteducation-parent
 * @description:
 * @author: 张玉森
 * @create: 2023-06-08 16:11
 **/
@Service
public class VideoServiceImpl implements VideoService {
    @Autowired
    private VideoMapper videoMapper;

    @Override
    public List<EduVideo> findAllVideo(String videoId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", videoId);
        wrapper.orderByAsc("sort");
        return videoMapper.selectList(wrapper);
    }

    @Override
    public Integer deleteById(String Id) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("id", Id);
        int i = videoMapper.deleteById(wrapper);
        return i;
    }

    @Override
    public Integer insertVideo(InsertVideoVo insertVideoVo) {
        EduVideo eduVideo = new EduVideo();
        eduVideo.setId(RandomUtil.randomNumbers(19));
        eduVideo.setCourseId(insertVideoVo.getCourseId());
        eduVideo.setChapterId(insertVideoVo.getChapterId());
        eduVideo.setTitle(insertVideoVo.getTitle());
        eduVideo.setVideoSourceId(insertVideoVo.getVideoSourceId());
        eduVideo.setVideoOriginalName(insertVideoVo.getVideoOriginalName());
        eduVideo.setSort(insertVideoVo.getSort());
        eduVideo.setIsFree(insertVideoVo.getIsFree());
        eduVideo.setStatus(1);
        eduVideo.setVersion(1L);
        eduVideo.setGmtCreate(LocalDateTime.now());
        eduVideo.setGmtModified(LocalDateTime.now());
        return videoMapper.insert(eduVideo);
    }

    @Override
    public Integer updateVideo(UpdataVideoVo updateVo) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("id", updateVo.getId());
        //wrapper.eq("course_id", updateVo.getCourseId());
        EduVideo eduVideo = new EduVideo();
        eduVideo.setIsFree(updateVo.getIsFree());
        eduVideo.setTitle(updateVo.getTitle());
        eduVideo.setGmtModified(LocalDateTime.now());
        if (!updateVo.getVideoSourceId().equals("")) {
            eduVideo.setVideoSourceId(updateVo.getVideoSourceId());
        }
        if (!updateVo.getVideoOriginalName().equals("")) {
            eduVideo.setVideoOriginalName(eduVideo.getVideoOriginalName());
        }
        return videoMapper.update(eduVideo, wrapper);
    }

    @Override
    public Integer deleteVideoById(String id, Integer sort, String chapterId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        wrapper.gt("sort", sort);
        List<EduVideo> eduVideos = videoMapper.selectList(wrapper);
        for (EduVideo eduVideo : eduVideos) {
            eduVideo.setSort(eduVideo.getSort() - 1);
            videoMapper.updateById(eduVideo);
        }
        EduVideo eduVideo = new EduVideo();
        eduVideo.setId(id);
        eduVideo.setChapterId(chapterId);
        return videoMapper.deleteById(eduVideo);
    }
}
